package aaaFace;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.attilax.json.AtiJson;
import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;

public class FaceCheckor {

	public static void main(String[] args) {
		int i=new FaceCheckor().faceNum("c:\\a.jpg");

	}

	public int faceNum(String imgpath) {
		String faces_json = getFacesJson(imgpath);
		String t = faces_json;
		// filex.read("e:\\x.json");
		t = t.replace("\n", "");
		t = t.replace("\r", "");
		t = t.replace("\t", "");
		Map m = AtiJson.fromJson(t);
		List<Map> li = (List) m.get("face");
		return li.size();

	}

	private String getFacesJson(String imgpath) {
		String f = imgpath;
		// replace api_key and api_secret here (note)
		HttpRequests httpRequests = new HttpRequests(
				"4480afa9b8b364e30ba03819f3e9eff5",
				"Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M ", true, true);
		System.out.println("FacePlusPlus API Test:");

		// detection/detect
		PostParameters postParameters = new PostParameters();
		postParameters.setImg(new File(f));
		// postParameters.setUrl("http://cn.faceplusplus.com/wp-content/themes/faceplusplus/assets/img/demo/9.jpg")
		JSONObject result;
		try {
			result = httpRequests.detectionDetect(postParameters);
		} catch (FaceppParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		String faces_json = result.toString();
		return faces_json;
	}

}
